package com.markerhub.controller;

import com.markerhub.common.lang.Result;
import com.markerhub.entity.SysDict;
import com.markerhub.service.SysDictService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Date;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

/**
 * 数据字典Controller
 */
@Api(tags = "数据字典管理")
@RestController
@RequestMapping("/api/sys-dict")
public class SysDictController extends BaseController {

    @Autowired
    private SysDictService sysDictService;

    /**
     * 获取数据字典列表
     */
    @PreAuthorize("hasAuthority('sys:dict:list')")
    @ApiOperation(value = "获取字典列表", notes = "获取所有数据字典信息，支持分页查询")
    @GetMapping("/list")
    public Result list(@RequestParam(required = false) String dictName, @RequestParam(required = false) String dictType) {
        // 使用BaseController中的getPage()方法获取分页参数
        Page<SysDict> page = getPage();
        // 构建查询条件
        QueryWrapper<SysDict> wrapper = new QueryWrapper<>();
        if (dictName != null && !dictName.isEmpty()) {
            wrapper.like("dict_name", dictName);
        }
        if (dictType != null && !dictType.isEmpty()) {
            wrapper.like("dict_type", dictType);
        }
        // 按创建时间倒序排序
        wrapper.orderByDesc("created");
        // 执行分页查询
        Page<SysDict> pageData = sysDictService.page(page, wrapper);
        return Result.succ(200, "获取数据字典列表成功", pageData);
    }
    
    /**
     * 查询数据字典
     */
    @PreAuthorize("hasAuthority('sys:dict:list')")
    @ApiOperation(value = "查询数据字典", notes = "根据条件查询数据字典")
    @GetMapping("/query")
    public Result query(@RequestParam(required = false) String dictName, 
                       @RequestParam(required = false) String dictType) {
        // 复用list方法的查询逻辑
        return list(dictName, dictType);
    }

    /**
     * 获取数据字典详情
     */
    @PreAuthorize("hasAuthority('sys:dict:list')")
    @ApiOperation(value = "获取数据字典详情", notes = "根据ID获取数据字典详细信息")
    @GetMapping("/detail/{id}")
    public Result detail(@PathVariable Long id) {
        SysDict dict = sysDictService.getById(id);
        return Result.succ(200, "获取数据字典信息成功", dict);
    }
    
    /**
     * 获取所有字典（用于下拉选择）
     */
    @ApiOperation(value = "获取所有字典", notes = "获取所有字典数据用于下拉选择")
    @GetMapping("/all")
    public Result all() {
        return Result.succ(200, "获取所有字典成功", sysDictService.list());
    }

    /**
     * 创建数据字典
     */
    @PreAuthorize("hasAuthority('sys:dict:add')")
    @ApiOperation(value = "创建数据字典", notes = "新增数据字典信息")
    @PostMapping("/add")
    public Result add(@RequestBody SysDict dict) {
        // 设置创建时间
        dict.setCreated(new Date());
        // 设置更新时间
        dict.setUpdated(new Date());
        boolean saved = sysDictService.save(dict);
        return saved ? Result.succ(200, "创建数据字典成功", null) : Result.fail("创建数据字典失败");
    }

    /**
     * 更新数据字典
     */
    @PreAuthorize("hasAuthority('sys:dict:update')")
    @ApiOperation(value = "更新数据字典", notes = "更新数据字典信息")
    @PutMapping("/update")
    public Result update(@RequestBody SysDict dict) {
        // 设置更新时间
        dict.setUpdated(new Date());
        boolean updated = sysDictService.updateById(dict);
        return updated ? Result.succ(200, "更新数据字典成功", null) : Result.fail("更新数据字典失败");
    }

    /**
     * 删除数据字典（支持单删和批量删除）
     */
    @PreAuthorize("hasAuthority('sys:dict:delete')")
    @ApiOperation(value = "删除数据字典", notes = "根据ID删除单个数据字典")
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Long id) {
        boolean deleted = sysDictService.removeById(id);
        return deleted ? Result.succ(200, "删除数据字典成功", null) : Result.fail("删除数据字典失败");
    }
    

}